﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using iTextSharp.text.pdf;
using iTextSharp.text;
using System.IO;

namespace XmlToPdf
{
    /// <summary>
    /// 利用Salary对象直接创建pdf，给pdf中添加内容
    /// 未使用xslt，
    /// 废弃
    /// </summary>
    public class CreatePdf
    {
        private const string XML_NODE_SALARY = "salary";
        private const string XML_NODE_NAME = "name";
        private const string XML_NODE_BASICSALARY = "basicSalary";
        private const string XML_NODE_JOBSALARY = "jobSalary";
        private const string XML_NODE_OLD = "old";
        private const string XML_NODE_POSTSALARY = "postSalary";
        private const string XML_NODE_OVERTIME = "overTime";
        private const string XML_NODE_TOTAL = "total";
        private const string XML_NODE_TEX = "tex";
        private const string XML_NODE_INSURANCE = "insurance";
        private const string XML_NODE_HOUSE = "house";
        private const string XML_NODE_OTHERS = "others";
        private const string XML_NODE_FACTSALARY = "factSalary";
        private const string XML_NODE_MEMO = "memo";
        private const string XML_NODE_OVERTIMENO = "overTimeNo";
        private const string XML_NODE_EMAIL = "email";
        private const string XML_NODE_SPECIAL = "special";

        public static void Create(string strAll, string salaryDate)
        {
            string str_salary_head = "{0}年{1}月工资表";
            str_salary_head = string.Format(str_salary_head,
                                            salaryDate.Substring(0, 4),
                                            salaryDate.Substring(4));

            List<Salary> lst = getXmlSalariesData(strAll);

            foreach (Salary salary in lst)
            {
                createSinglePdf(salary, str_salary_head);
            }
            
        }

        private static void createSinglePdf(Salary salary, string str_salary_head)
        {            

            Font chineseFont = getFont();

            Document doc = new Document();

            PdfWriter.GetInstance(doc, new FileStream(
                                           "../server/XmlToPdf/" + salary.name + ".pdf", 
                                           FileMode.Create));

            doc.Open();

            Paragraph para = new Paragraph(str_salary_head, chineseFont);
            doc.Add(para);

            PdfPTable table = new PdfPTable(7);

            #region 向单元格中添加数据
            PdfPCell cell11 = new PdfPCell(new Phrase("姓名", chineseFont));
            table.AddCell(cell11);
            PdfPCell cell12 = new PdfPCell(new Phrase("基本工资", chineseFont));
            table.AddCell(cell12);
            PdfPCell cell13 = new PdfPCell(new Phrase("岗位工资", chineseFont));
            table.AddCell(cell13);
            PdfPCell cell14 = new PdfPCell(new Phrase("特殊津贴", chineseFont));
            table.AddCell(cell14);
            PdfPCell cell15 = new PdfPCell(new Phrase("工龄工资", chineseFont));
            table.AddCell(cell15);
            PdfPCell cell16 = new PdfPCell(new Phrase("职务工资", chineseFont));
            table.AddCell(cell16);
            PdfPCell cell17 = new PdfPCell(new Phrase("加班费", chineseFont));
            table.AddCell(cell17);

            PdfPCell cell21 = new PdfPCell(new Phrase(salary.name, chineseFont));
            table.AddCell(cell21);
            PdfPCell cell22 = new PdfPCell(new Phrase(salary.basicSalary.ToString()));
            table.AddCell(cell12);
            PdfPCell cell23 = new PdfPCell(new Phrase(salary.jobSalary.ToString()));
            table.AddCell(cell23);
            PdfPCell cell24 = new PdfPCell(new Phrase(salary.special.ToString()));
            table.AddCell(cell24);
            PdfPCell cell25 = new PdfPCell(new Phrase(salary.old.ToString()));
            table.AddCell(cell25);
            PdfPCell cell26 = new PdfPCell(new Phrase(salary.postSalary.ToString()));
            table.AddCell(cell26);
            PdfPCell cell27 = new PdfPCell(new Phrase(salary.overTime.ToString()));
            table.AddCell(cell27);

            PdfPCell cell31 = new PdfPCell(new Phrase("应付小计", chineseFont));
            table.AddCell(cell31);
            PdfPCell cell32 = new PdfPCell(new Phrase("所得税", chineseFont));
            table.AddCell(cell32);
            PdfPCell cell33 = new PdfPCell(new Phrase("社保", chineseFont));
            table.AddCell(cell33);
            PdfPCell cell34 = new PdfPCell(new Phrase("住房公积金", chineseFont));
            table.AddCell(cell34);
            PdfPCell cell35 = new PdfPCell(new Phrase("其他", chineseFont));
            table.AddCell(cell35);
            PdfPCell cell36 = new PdfPCell(new Phrase("实发金额", chineseFont));
            table.AddCell(cell36);
            PdfPCell cell37 = new PdfPCell(new Phrase(""));
            table.AddCell(cell37);


            PdfPCell cell41 = new PdfPCell(new Phrase(salary.total.ToString()));
            table.AddCell(cell41);
            PdfPCell cell42 = new PdfPCell(new Phrase(salary.tex.ToString()));
            table.AddCell(cell42);
            PdfPCell cell43 = new PdfPCell(new Phrase(salary.insurance.ToString()));
            table.AddCell(cell43);
            PdfPCell cell44 = new PdfPCell(new Phrase(salary.house.ToString()));
            table.AddCell(cell44);
            PdfPCell cell45 = new PdfPCell(new Phrase(salary.others.ToString()));
            table.AddCell(cell45);
            PdfPCell cell46 = new PdfPCell(new Phrase(salary.factSalary.ToString()));
            table.AddCell(cell46);
            PdfPCell cell47 = new PdfPCell(new Phrase(""));
            table.AddCell(cell47);
            #endregion

            doc.Add(table);

            doc.Close();
        }

        /// <summary>
        /// 解析xml字符串，得到数据对象
        /// </summary>
        /// <param name="encrptedData">xml字符串</param>
        /// <returns>数据对象</returns>
        private static List<Salary> getXmlSalariesData(string xmlStr)
        {
            // xml文件内容解析
            XDocument xdoc = XDocument.Parse(xmlStr);
            try
            {
                var xmlSalaries = from salary in xdoc.Descendants(XML_NODE_SALARY)
                                  where salary.Element(XML_NODE_NAME) != null
                                  select new Salary
                                  {
                                      name = (string)salary.Element(XML_NODE_NAME),
                                      basicSalary = (double)salary.Element(XML_NODE_BASICSALARY),
                                      jobSalary = (double)salary.Element(XML_NODE_JOBSALARY),
                                      old = (double)salary.Element(XML_NODE_OLD),
                                      postSalary = (double)salary.Element(XML_NODE_POSTSALARY),
                                      overTime = (double)salary.Element(XML_NODE_OVERTIME),
                                      total = (double)salary.Element(XML_NODE_TOTAL),
                                      tex = (double)salary.Element(XML_NODE_TEX),
                                      insurance = (double)salary.Element(XML_NODE_INSURANCE),
                                      house = (double)salary.Element(XML_NODE_HOUSE),
                                      others = (double)salary.Element(XML_NODE_OTHERS),
                                      factSalary = (double)salary.Element(XML_NODE_FACTSALARY),
                                      memo = (string)salary.Element(XML_NODE_MEMO),
                                      overTimeNo = (double)salary.Element(XML_NODE_OVERTIMENO),
                                      email = (string)salary.Element(XML_NODE_EMAIL),
                                      special = (double)salary.Element(XML_NODE_SPECIAL)
                                  };
                return xmlSalaries.ToList<Salary>();
            }
            catch (Exception e)
            {
                return new List<Salary>();
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private static Font getFont()
        {
            BaseFont.AddToResourceSearch("../server/XmlToPdf/lib/iTextAsian.dll");
            BaseFont.AddToResourceSearch("../server/XmlToPdf/lib/iTextAsianCmaps.dll");
            BaseFont font = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
            Font chineseFont = new Font(font);
            return chineseFont;
        }
    }
}
